import axios from 'axios'
import commonUrl from '../common/js/commonUrl.js'
import { loadUserId, loadVillageId } from '../common/js/cache.js'
// import { loadVillageId, loadUserId } from '../common/js/cache.js'
import qs from 'qs'

// 判断是否为工作人员
export function isWorker () {
  const data = {
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/Worker`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 去申请为工作人员
export function applyWorker () {
  const data = {
    user_id: loadUserId(),
    areas_id: loadVillageId()
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/apply`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 显示投票列表
export function getVoteList () {
  const data = {
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/getVote`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 选择 苑 幢 单元
export function getAreas (id) {
  const data = {
    areas_id: id
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/voteHousr`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 选择单元后去显示投票结果
export function getVote (did, vid) {
  const data = {
    danyuan_id: did,
    vote_id: vid
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/voteRoomNum`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 确认提交投票结果
export function submitVote (aid, vid, datas) {
  const data = {
    areas_id: aid,
    vote_id: vid,
    data: datas
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/setVoteRoom`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 查看小区单元投票结果
export function lookVote (did, vid) {
  const data = {
    danyuan_id: did,
    vote_id: vid
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/alreadyVote`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 取消投票
export function deleteVote (id) {
  const data = {
    id
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/cancelVote`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 点击投票小区列表已投票查看全部投票
export function lookAlreadyVote (aid, id) {
  const data = {
    areas_id: aid,
    vote_id: id
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/getAllVote`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 点击小区列表已送票查看全部送票
export function lookSendVote (id) {
  const data = {
    vote_id: id
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/getGiveVote`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 点击小区列表已回收情况
export function lookRecycleVote (id) {
  const data = {
    vote_id: id
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/Votestatistics/getRecoveryVote`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
